import { syncSetTimeout, sendMessage, getMessage, dispatch } from "../../global/tools.js";

export default class CheckPayment {
    static count = 0;
    constructor(webController) {
        CheckPayment.count++;
        if (CheckPayment.count >= 2) {
            alert("collectionPlugin: \nError: CheckPayment.count >= 2，即将刷新页面。");
            location.reload(true);
        } 
        this.webController = webController;
        this.ah = {};
        this.init();
    }
    init() {
        // 添加功能按钮
        {
            jQuery("ul.list").append(`<li style="color: white; margin-left: 30px; font-size: 18px;"><button id="collectionPlugin-checkPayment-checkPayment" class="ivu-btn ivu-btn-primary">查询未结清案件</button></li>`); 
            jQuery("#collectionPlugin-checkPayment-checkPayment").click(() => {
                this.runHook();
                jQuery("#content-wrapper div.mainbox.searchbox form button:contains('查询')").eq(0).click();
            });
        }  
        // 监听消息
        getMessage("anmi_checkPayment", message => {
            switch (message.title) {
                case "checkPayment":
                    let num1 = 0;
                    let num2 = message.data.length;
                    message.data.forEach(element => {
                        element.isPay ? null : num1++;
                    });
                    this.setNotice(num1, num2, message.data);
                    break;
                default:
                    break;
            }
        });
    }
    runHook() {
        this.ah = ah.proxy({
            onResponse: (response, handler) => {
                let result = [];
                if (response.config.url === "/xapi/v1/cases/list") {
                    let res = JSON.parse(response.response);
                    if (res.status === 200) {
                        let data = res.data;
                        data.list.forEach(element => {
                            result.push({
                                mobile: element.mobile,
                                shopId: element.fieldJson.c3982d4_828,
                                isPay: true
                            });
                        });
                        if (result.length > 100) {
                            VueMainInstance.$Notice.close("checkPayment");
                            VueMainInstance.$Notice.open({
                                name: "checkPayment",
                                title: `最多支持同时查询100条案件。`, 
                                duration: 10
                            })
                        } else {
                            sendMessage({
                                from: "anmi_checkPayment",
                                to: "background",
                                toUrl: "background",
                                title: "checkPayment",
                                data: result
                            });
                            VueMainInstance.$Notice.close("checkPayment");
                            VueMainInstance.$Notice.open({
                                name: "checkPayment",
                                title: `待查询案件总数${result.length},正在查询，请等待...`, 
                                duration: 0
                            })
                        }
                    }
                }
                this.unHook();
                handler.next(response);
            }
        })
    }
    unHook() {
        this.ah.unProxy();
    }
    setNotice(num1, num2, list) {
        VueMainInstance.$Notice.close("checkPayment");
        VueMainInstance.$Notice.open({
            name: "checkPayment",
            title: `查询到逾期信息(不含乐e付)：${num1}/${num2}`, 
            duration: 0,
            render: h => {
                return h('a', { 
                    onclick: () => {
                        let mobiles = [];
                        list.forEach(element => {
                            element.isPay ? null : mobiles.push(element.mobile);
                        });
                        let str = mobiles.join("\n");
                        const targetBtn1 = jQuery("div.custom-search").next().find("span:contains('重置')").parent("div.ivu-form-item-content > button");
                        if (targetBtn1.length != 1) {
                            console.error("targetBtn.length != 1");
                            return;
                        } else {
                            dispatch(targetBtn1, "click");
                        }
                        let target = jQuery("div.search-item > div.title-item > span.title:contains('手机号')").parent("div.title-item").next("div.ivu-tooltip").find("textarea");
                        if (target.length != 1) {
                            console.error("target.length != 1");
                            return;
                        }
                        target.val(str);
                        dispatch(target, "input");
                        dispatch(target, "change");
                        const targetBtn2 = jQuery("div.custom-search").next().find("span:contains('查询')").parent("div.ivu-form-item-content > button");
                        if (targetBtn2.length != 1) {
                            console.error("targetBtn.length != 1");
                            return;
                        } else {
                            dispatch(targetBtn2, "click");
                        }
                    }
                }, '点击这里搜索未还款合同')
            }
        })
    }
}